<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.net.URLDecoder,cn.edu.nju.ws.sview.Config,cn.edu.nju.ws.sview.views.Comment,cn.edu.nju.ws.sview.views.Image,cn.edu.nju.ws.sview.views.Group,cn.edu.nju.ws.sview.util.URIUtil,java.net.URLEncoder,java.util.Enumeration,java.util.ArrayList,cn.edu.nju.ws.sview.util.URIUtil,cn.edu.nju.ws.sview.views.Entity,cn.edu.nju.ws.sview.views.data.EntityManagement,cn.edu.nju.ws.sview.views.data.EntityManagement.URIMember,cn.edu.nju.ws.sview.cache.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>SView Identity</title>
<link rel="icon" href="/sview/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/sview/css/layout.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/smoothness/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/views/identity.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/easyui.css" />
<script type="text/javascript" src="/sview/script/jquery.js"></script>
<script type="text/javascript" src="/sview/script/jquery.easyui.min.js"></script>
<script type="text/javascript" src="/sview/script/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="/sview/script/global.js"></script>
<script type="text/javascript" src="/sview/script/views/identity.js"></script>
</head>
<body>
<%
	String lang = request.getParameter("lang");
		if (lang != null && lang.equals("zh")) {
	request.getRequestDispatcher("identity_zh.jsp").forward(request,
	response);
	return;
		}else if(lang==null){
	lang="en" ;
		}
		
		String genre = request.getParameter("genre");
		String eidStr =  request.getParameter("eid");
		int eid =0;
		int etype=0;
		if(genre==null){
	String location = "../views/index.jsp";
	if(lang.equals("zh"))
		location = location+"?lang="+lang;
	response.sendRedirect(location);
	return;
		}else{
	eid = Integer.parseInt(eidStr);
		}
		if(genre.equals(Entity.URI_ENTITY_Str))
	etype = Entity.URI_ENTITY;
		else if(genre.equals(Entity.CUSTOM_ENTITY_Str))
		etype = Entity.CUSTOM_ENTITY;
		else if(genre.equals(Entity.COLLECTION_Str))
	etype = Entity.COLLECTION;
		
		if(!genre.equals(Entity.URI_ENTITY_Str)){
	String testName = Entity.getNameByEntityID(eid,"en");
	if(testName==null){
		String location = "eerror.jsp";
		if(lang.equals("zh"))
	location = location+"?lang="+lang;
		response.sendRedirect(location);
		return;
	}
		}
		
		String gname = null;
		if(session.getAttribute("groupName")!=null){
	gname = (String)session.getAttribute("groupName");
		}else{
	String location = "../views/index.jsp";
	if(lang.equals("zh"))
		location = location+"?lang="+lang;
	response.sendRedirect(location);
	return;
		}
		
		int gid =Group.getGroupIdByName(gname);
		
		String ename = request.getParameter("n");
		if(ename!=null){
	ename = URLDecoder.decode(ename,"utf-8");
		}
%>
<jsp:include page="/vheader.jsp" flush="true">
   <jsp:param name="f" value="e"/>
</jsp:include>
<%
	boolean existFlag = false;
%>
<%
	//对于原始uri named entity，查看所在编辑组是否已经定制过实体，并提示转到那个实体的编辑页面中
	if(request.getParameter("direct")==null&&genre.equals(Entity.URI_ENTITY_Str)){
		int custEntityID = EntityManagement.getInstance()
	.isCreated(etype,eid,gid,null);
		if(custEntityID>0){
	existFlag = true;
		}
		if(existFlag){
	String custEntityName = Entity.getNameByEntityID(custEntityID,"en");
%>
   <div class="changeBox"> 
     <div class="warn"></div>
     <div class="text">
         <div class="d1">Your editing group has created the custom entity <span class="ename" eid="<%=custEntityID%>"><%=custEntityName%></span> on this uri.</div>
         <div class="d2"> Go to edit that custom enity now?</div>
         <div class="op"><input type="button" class="yes" value="Yes" /> <input type="button" class="no" value="No" /></div>
     </div>
   </div>
<%
	}
	}
%>
<%
	if(!existFlag){
%>
<div class="container">
   <div class="top">
      <div class="mspacing"></div>
      <%
      	if(genre.equals(Entity.CUSTOM_ENTITY_Str)){
      %>
      <div class="operation">Edit Custom Entity</div>
      <%
      	}else{
      %>
       <div class="operation">Create Custom Entity</div>
      <%
      	}
      %>
      <div class="edit-save"> 
         <input type="button" value="Save" class="sbutton btn btn-primary fb">
         <input type="button" value="Cancel" class="cbutton btn fb">
       </div>
   </div>
   <div class="mspacingLittle"></div>
      <div class="main">
	     <div class="entityName section">
	     	<div class="tab-header">
	           <div class="tab" >
			       <span class="title">Basic Information</span>
			       <span class="tab-status"></span>
			    </div>
	        </div>
			<div class="content">
			    <div class='namebox'>
                   <span class="label">Name:</span>
                   <span>
                      <%
                      	if(genre.equals(Entity.CUSTOM_ENTITY_Str)){
                                                                      	 if(ename==null){
                                                                     	 	ename = Entity.getNameByEntityID(eid,"en");
                                                                     	 }
                      %>
                            <input type="text" class="ftext inename" value="<%=ename%>" >
                      <%
                      	}else{ 
                                                                	   if(ename==null){
                                                                		  try{
                                                                   	 	  	ename = URILabelCache.getInstance().getLabel(eid,lang);
                                                                		  }catch(Throwable e){
                                                                			  e.printStackTrace();
                                                                		  }
                                                                   	   }
                      %>
                        	<input class="ftext inename" value="<%=ename%>" type="text">
                       <%
                       	}
                       %>
                   </span>
                 </div>
               
                 <div class='descbox'>
                    <span class="label">Description:</span>
                    <span>
                    <%
                    	String comment = null;
                                                               if(genre.equals(Entity.CUSTOM_ENTITY_Str)){
                                                           	     Comment commObj =  Comment.getCommentByMemberLangGroup(etype,eid,lang,gname);
                                        	           		     if(commObj!=null){
                                        	           		         comment = commObj.getText();
                                        	           		     }
                                                               }else{
                                                            	  try{
                                               		    	   			String uri = URIIDCache.getInstance().getURI(eid) ;
                                               		    	   			comment  = Comment.getOriginComment(uri,lang) ;
                                               		    	 	  }catch(Throwable e){
                                               		    		 		e.printStackTrace();
                                               		    	 	  }  
                                                               }
                    %>
                    <%
                    	if(comment!=null&&!comment.trim().equals("")){
                    %>
                    	<textarea class="ftext commentarea "><%=comment%></textarea>
                    <%
                    	}else{
                    %>
                     	<textarea class="ftext commentarea "></textarea>
                    <%
                    	}
                    %>
                    </span>
                </div>
                <div class="image">
                    <span class="label">Image:</span>
                  <div class="content">
				    <div class="alignr"><a class="addImage" href="javascript:void(0)" >Submit an Image URI</a></div>
				    <div class="imgList">
				    <%
				    	ArrayList<String> imgPath = new ArrayList<String>();
				    		    		      ArrayList<Integer> imgIDs = new ArrayList<Integer>();
				    		    	          if(genre.equals(Entity.URI_ENTITY_Str)){
				    		    	       		try{
				    		    	    	   		String uri = URIIDCache.getInstance().getURI(eid) ;
				    		    	    	  		String img_path  = Image.getOriginImage(uri);
				    		    	    	  		if(!img_path.trim().equals("")){
				    		    	    	  			imgPath.add(img_path);
				    		    	    	  		}
				    		    	    	 	}catch(Throwable e){
				    		    	    		 	e.printStackTrace();
				    		    	    	 	}
				    		    	          }else{
				    		    	       		String uri = null;
				    		    	  	   		ArrayList<Image> imgArray  =Image.getAllImagesByEntityGroupOrderByTime(etype,eid,gname);
				    		    	  	   		if(imgArray!=null&&imgArray.size()>0){
				    		    	  	   		    for(Image img:imgArray){
				    		    	  	   		    	imgPath.add(img.getPath());
				    		    	  	   		        imgIDs.add(img.getID());
				    		    	  	   		    }
				    		    	       		}
				    		    	          }            
				    		    	          for(int i=imgPath.size()-1;i>=0;i--){
				    		    			   String mpath = imgPath.get(i);
				    %>
				     <div class="imgBox origin">
				    	<div class="img-container">
				    	  <%
				    	  	if(genre.equals(Entity.URI_ENTITY_Str)){
				    	  %>
				          <img src="<%=mpath%>" alt="load image failure">
				          <%
				          	}else{
				          		          		        	   int imgID = imgIDs.get(i);
				          %>
				            <img src="<%=mpath%>" id="<%=imgID%>" alt="load image failure">
				          <%
				          	}
				          %>
				         </div>
				         <a class="imgDelete"><span class="img-delete"></span></a>
				       </div>
				    <%
				    	}
				    %>
				    </div>
				    <div class="imgRemoved hidden"></div>
			      </div>
                </div>
                <div class="clear"></div>
             </div>
		 </div>
	     <div class="identity section">
		    <div class="tab-header">
	           <div class="tab" >
			       <span class="title">Identity </span>
			        <span class="tab-status"></span>
			    </div>
	        </div>
			<div class="content">
				<div class="leftPanel">
					<div class="entity-list">
					</div>
					<div class="advancedBox">
					    <a  href="javascript:void(0)" class="advancedSwitch">Add URI manually</a>
					    <div class="content">
					    	<div class='header'>Enter a uri to use as member:</div>
					    	<div><input id="uriAdded" type="text" size="75" value="http://"  class ="ftext"></div>
					    	<div class="opBox">
					    	<input type="button" value="Ok" class="addbtn" id="addbtn" />
					    	<input type="button" value="Reset" class="rstbtn" id="rstbtn" />
					    	</div>
					       <div class="input-status">
					    	  <div class="wait sts">
					    		  <div class="hint"><span>Checking...</span></div>
					    	  </div>
					    	  <div class="valid sts"></div>
					    	  <div class="invalid sts"><img src="/sview/icons/warning.png"></img>Sorry, SView did not find RDF description about the uri.</div>
					       </div>					    	
					    </div>
					</div>
				</div>
				<div class='rightPanel'>
				   <div class="recommendSection">
			        <ul>
		               <li><a href="#tabs-objcoref">ObjectCoref</a></li>
		               <li><a href="#tabs-search">Falcons Search</a></li>
		               <li><a href="#tabs-removed">Removed</a></li>
	                </ul>
	                <div id="tabs-objcoref">
	                  	<div class="content">
	                  	   <ul class="entityList"></ul>
	                  	</div>
	                 </div>
                    <div id="tabs-search">
                        <div class="searchBox">
                            <input id="query" type="text" size="75" value=""  class ="ftext" name="query">
         	                <input type="button" value="Search" class="sbtn" id="searchq">
                        </div>                    
	                  	<div class="content">
	                  	   <ul class="entityList"></ul>
	                  	</div>
	                 </div>	                 
	                <div id="tabs-removed">
	                  	<div class="content">
	                  	   <ul class="entityList"></ul>
	                  	</div>
	                 </div>
				    </div>
				   </div>
			</div>
		 </div>	 
	   </div>
</div>
<div class="foot"></div>
<input type="hidden" id="gid" value="<%=gid%>" />
<input type="hidden" id="gname" value="<%=gname%>" />
<div class="hidden">
	<div id="image-op" title="Submit Image URI" >
    	<div class="title">Please input the image URI</div>
    	<div class="inputContainer">
      	<input id="imgURI" class="type-file-text" type="text">
   		</div>
    	<div class="img_hint">Prompt: please make sure the URI is valid</div>
    	<div class="status"><img id="loading"  src="/sview/icons/loading.gif"></div>
    	<div class="error"></div>
   		<div class="upload-btn">
      		<a  href="javascript:void(0)">
        		<span class="button-bg">
         	 		<span class="upload">Submit</span>
         		</span>
      		</a>
    	</div>
	</div>
</div>	
<%
		if(ename!=null){
	%>
<input type="hidden" id="ename" value="<%=ename%>" />
<%
	}
%>
<div class="warning">
	<div class="warnBox">
	    <p>Your editing group has created the custom entity <span class="ename"></span> on this uri.</p>
	    <p>This uri cannot be member of the current custom entity under SView constraint.</p> 
	    <p>Go to edit <span class="ename"></span> now? </p> 
	</div>
</div>
<%
	}
%>
<input type="hidden" id="display-lang" value="en" />
<input type="hidden" id="genre" value="<%=genre%>" />
<%
	if(genre.equals(Entity.CUSTOM_ENTITY_Str)){
%>
<input type="hidden" id="custom_type" value="edit" />
<%}else {%>
<input type="hidden" id="custom_type" value="create" />
<%} %>
<input type="hidden" id="etype" value="<%=etype %>" />
<input type="hidden" id="eid" value="<%=eid %>" />
<jsp:include page="/vfooter.jsp"></jsp:include>
</body>
</html>